import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FindAnagrams {

    public List<Integer> findAnagrams(String s, String p) {
        int n = s.length();
        int m = p.length();
        if (n < m) return Collections.EMPTY_LIST;
        List<Integer> ans = new ArrayList<>();
        char[] chars1 = s.toCharArray();
        char[] chars2 = p.toCharArray();
        int[] nums1 = new int[26];
        int[] nums2 = new int[26];
        for (int i = 0; i < m; i++) {
            ++nums1[chars1[i] - 'a'];
            ++nums2[chars2[i] - 'a'];
        }
        if (Arrays.equals(nums1, nums2)) ans.add(0);
        for (int i = m; i < n; i++) {
            ++nums1[chars1[i] - 'a'];
            --nums1[chars1[i - m] - 'a'];
            if (Arrays.equals(nums1, nums2)) {
                ans.add(i - m + 1);
            }
        }
        return ans;
    }
}
